﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using Common.Logging;
using Utility;

namespace AutomationTestCases.AutomationTestCommon
{
    public class AutomationOperation
    {
        private const int sleepTime = 1000;
        protected static ILog log = LogFactory.CreateFileLog();

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);

        [DllImport("user32.dll", SetLastError = true)]
        static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);

        [DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
        private extern static IntPtr FindWindow(string lpClassName, string lpWindowName);

        //处理网页错误
        protected static void FixForServerOSWindowsClick()
        {
            var winForm = FindWindow("#32770", "Message from webpage");
            if (null != winForm)
            {
                log.Info(string.Format("this window IntPtr is {0}", winForm));
                var button = FindWindowEx(winForm, IntPtr.Zero, "Button", "OK");
                uint message = 0xf5;
                SendMessage(button, message, IntPtr.Zero, IntPtr.Zero);
            }
        }

        public static void ClickByClassNameFilterWithText(AutomationTestBase browser, string className, string text)
        {
            try
            {
                FixForServerOSWindowsClick();
                var elements = browser.FindElementsByClassName(className);
                if (elements != null)
                {
                    foreach (var ele in elements)
                    {
                        if (ele != null && ele.Text.Trim().Equals(text, StringComparison.InvariantCultureIgnoreCase))
                        {
                            log.Info(string.Format("class:[{0}],Text:[{1}]", className, ele.Text));
                            ele.Click();
                            break;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                log.Error(string.Format("Error Message: {0}\r\nStack Trace: {1}", ex.Message, ex.StackTrace));
            }
        }

        public static void SelectByIDFilterWithText(AutomationTestBase browser, string id, string text)
        {
            try
            {
                FixForServerOSWindowsClick();
                var elements = browser.FindElementsById(id);
                if (elements != null)
                {
                    foreach (var ele in elements)
                    {
                        if (ele != null && ele.Text.Trim().Equals(text, StringComparison.InvariantCultureIgnoreCase))
                        {
                            log.Info(string.Format("id:[{0}],text:[{1}]", id, ele.Text));
                            ele.Select();
                            break;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                log.Error(string.Format("Error Message: {0}\r\nStack Trace: {1}", ex.Message, ex.StackTrace));
            }
        }

        public static void InputTextControlByID(AutomationTestBase browser, string id, string value)
        {
            try
            {
                FixForServerOSWindowsClick();
                var control = browser.FindElementById(id);
                if (control != null)
                {
                    control.Clear();
                    control.SendKeys(value);

                }
            }
            catch (Exception ex)
            {
                log.Error(string.Format("Error Message: {0}\r\nStack Trace: {1}", ex.Message, ex.StackTrace));
            }
        }

        public static void ControlClickByID(AutomationTestBase browser, string id)
        {
            try
            {
                FixForServerOSWindowsClick();
                var control = browser.FindElementById(id);
                if (control != null)
                {
                    control.Click();
                }
            }
            catch (Exception ex)
            {
                log.Error(string.Format("Error Message: {0}\r\nStack Trace: {1}", ex.Message, ex.StackTrace));
            }
        }

        public static void ControlClickByName(AutomationTestBase browser, string name)
        {
            try
            {
                FixForServerOSWindowsClick();
                var control = browser.FindElementByName(name);
                if (control != null)
                {
                    control.Click();
                }
            }
            catch (Exception ex)
            {
                log.Error(string.Format("Error Message: {0}\r\nStack Trace: {1}", ex.Message, ex.StackTrace));
            }
        }
    }
}
